﻿using AutoMapper;
using Modules.Corp;
using Modules.Device.Entitys;
using Modules.Project;
using PmSoft;
using System.ComponentModel.DataAnnotations;
using static Modules.Device.CheckInDeviceConstants;

namespace ManagementApi.Models.Device
{
    public class CheckInDeviceEditForm
    {
        /// <summary>
        /// 主键 
        ///</summary>
        [Display(Name = "主键")]
        public int? Id { get; set; }
        /// <summary>
        /// 企业id 
        ///</summary>
        [Display(Name = "企业id")]
        [Required(ErrorMessage = "{0}不能为空")]
        public int CorpId { get; set; }
        /// <summary>
        /// 项目id 
        ///</summary>
        [Display(Name = "项目id")]
        [Required(ErrorMessage = "{0}不能为空")]
        public int ProjectId { get; set; }
        /// <summary>
        /// 设备类型(海清......) 
        ///</summary>
        [Display(Name = "设备类型")]
        [Required(ErrorMessage = "{0}不能为空")]
        public 考勤设备设备厂家 Type { get; set; }
        /// <summary>
        /// 设备名称 
        ///</summary>
        [Display(Name = "设备名称")]
        [Required(ErrorMessage = "{0}不能为空")]
        public string Name { get; set; }
        /// <summary>
        /// 设备序列号(设备编号) 
        ///</summary>
        [Display(Name = "设备序列号")]
        [Required(ErrorMessage = "{0}不能为空")]
        public string Code { get; set; }
        /// <summary>
        /// 有效起始日期 
        ///</summary>
        [Display(Name = "有效起始日期")]
        [Required(ErrorMessage = "{0}不能为空")]
        public DateTime StartDate { get; set; }
        /// <summary>
        /// 有效截至日期 
        ///</summary>
        [Display(Name = "有效截至日期")]
        [Required(ErrorMessage = "{0}不能为空")]
        public DateTime EndDate { get; set; }
        /// <summary>
        /// 进出(1 进  2 出) 
        ///</summary>
        [Display(Name = "进出状态")]
        [Required(ErrorMessage = "{0}不能为空")]
        public 进出 InOut { get; set; }
        /// <summary>
        /// 设备型号 
        ///</summary>
        [Display(Name = "设备型号")]
        public string? Model { get; set; }
        /// <summary>
        /// 设备版本 
        ///</summary>
        [Display(Name = "设备版本")]
        public string? Version { get; set; }
        /// <summary>
        /// 备注 
        ///</summary>
        [Display(Name = "备注")]
        public string? Remark { get; set; }
        /// <summary>
        /// 是否禁用 
        ///</summary>
        [Display(Name = "是否禁用")]
        public bool? IsDisable { get; set; }

        #region 扩展属性
        public string? ProjectName { get; set; }

        public string? CorpName { get; set; }
        #endregion
    }

    public static partial class ProjectCheckInDeviceFormExceptions
    {

        public static CheckInDeviceEditForm AsForm(this CheckinDevice entity)
        {
            CheckInDeviceEditForm deviceEditForm = new CheckInDeviceEditForm();
            var mapper = ServiceAccessor.GetService<IMapper>();
            mapper.Map(entity, deviceEditForm);

            var corpService = ServiceAccessor.GetService<CorpService>();
            deviceEditForm.CorpName = corpService.GetCorpInfo(entity.CorpId)?.Name ?? string.Empty;
            var projectService = ServiceAccessor.GetService<ProjectService>();
            deviceEditForm.ProjectName = projectService.GetProjectInfo(entity.ProjectId)?.Name ?? string.Empty;
            return deviceEditForm;
        }
    }
}
